/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.qihoo.gamecenter.sdk.common.web.view;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Handler;
import android.os.Message;
import android.view.ViewGroup.LayoutParams;
import android.webkit.SslErrorHandler;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import com.qihoo.gamecenter.sdk.common.i.c;
import com.qihoo.gamecenter.sdk.common.i.q;
import java.io.File;
import java.lang.ref.WeakReference;

public class WebViewer extends FrameLayout {
    private static final String TAG = "WebViewer";
    private static final int MSG_COOKIES_SYNCED = 0;
    private static final int MSG_LOAD_TIMEOUT = 1;
    private static final int CHECK_COOKIES_INTERNAL = 100;
    private static final int MAX_TRY_TIMES = 8;
    private static final long LOAD_TIMEOUT = 20000L;
    private int mTryTimes = 1;
    private boolean mIsSynedMsgRemoved = false;
    private boolean mIsTimeoutMsgRemoved = false;
    private boolean mReloadAble = true;
    private Activity mContainer;
    private WebViewEx mWebView;
    private String mReqUrl;
    private NotOpenView mFailedOpenView;
    private com.qihoo.gamecenter.sdk.common.web.control.a mNetController;
    private String[] mCookies;
    private String mExpires;
    private String mPath;
    private String mDomain;
    private Handler mHandler = new a(this);
    private e mWebViewClient = new e();
    private d mWebChromeClient = new d();

    public WebViewer(Activity paramActivity) {
        super(paramActivity);
        this.mContainer = paramActivity;
        initWebview();
        initFailedView();
    }

    @SuppressLint({ "SetJavaScriptEnabled" })
    private void initWebview() {
        this.mWebView = new WebViewEx(this.mContainer);
        this.mWebView.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
        this.mWebView.setScrollBarStyle(33554432);
        this.mWebView.setWebViewClient(this.mWebViewClient);
        this.mWebView.setWebChromeClient(this.mWebChromeClient);
        this.mWebView.setVisibility(8);
        WebSettings localWebSettings = this.mWebView.getSettings();
        localWebSettings.setJavaScriptEnabled(true);
        localWebSettings.setDomStorageEnabled(true);
        localWebSettings.setDatabaseEnabled(true);
        localWebSettings.setAppCacheEnabled(true);
        localWebSettings.setAppCacheMaxSize(8388608L);
        String str1 = this.mContext.getDir("sdkWebCaches", 0).getPath();
        localWebSettings.setAppCachePath(str1);
        localWebSettings.setCacheMode(-1);
        localWebSettings.setDomStorageEnabled(true);
        localWebSettings.setDatabaseEnabled(true);
        String str2 = this.mContext.getDir("sdkWebDatabases", 0).getPath();
        localWebSettings.setDatabasePath(str2);
        localWebSettings.setSavePassword(false);
        localWebSettings.setSaveFormData(false);
        localWebSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        addView(this.mWebView);
    }

    private void initFailedView() {
        this.mFailedOpenView = new NotOpenView(this.mContainer);
        this.mFailedOpenView.setVisibility(8);
        addView(this.mFailedOpenView);
        if (this.mNetController != null)
            return;
        this.mNetController = new com.qihoo.gamecenter.sdk.common.web.control.a(this.mFailedOpenView);
        this.mNetController.a(this.mContainer);
    }

    private void showTimeOutView() {
        if (this.mWebView.getProgress() > 40)
            return;
        loadFailedView(new NotOpenView.c() {
            public void a() {
                WebViewer.this.loadUrl(WebViewer.this.mReqUrl);
            }
        });
    }

    public void loadFailedView(NotOpenView.c paramc) {
        try {
            this.mWebView.stopLoading();
            this.mWebView.setVisibility(8);
        } catch (Exception localException) {}
        this.mFailedOpenView.setOnRefreshWebview(paramc);
        this.mFailedOpenView.a();
        this.mFailedOpenView.setVisibility(0);
    }

    private void trySyncedCookie() {
        if (this.mTryTimes >= 8) {
            c.a("WebViewer", new Object[] { "Try Time out, go" });
            com.qihoo.gamecenter.sdk.common.web.a.a(this.mWebView, this.mReqUrl);
        }
        else if (com.qihoo.gamecenter.sdk.common.web.a.a(this.mReqUrl)) {
            c.a("WebViewer", new Object[] { "Cookies is synced, go" });
            com.qihoo.gamecenter.sdk.common.web.a.a(this.mWebView, this.mReqUrl);
        }
        else {
            this.mTryTimes += 1;
            this.mHandler.sendEmptyMessageDelayed(0, 100L);
            c.a("WebViewer", new Object[] { "Cookies is not synced, wait" });
        }
    }

    public void loadUrl(String paramString) {
        this.mReqUrl = paramString;
        boolean bool = com.qihoo.gamecenter.sdk.common.web.a.a(this.mContainer, paramString, this.mCookies,
                this.mExpires, this.mPath, this.mDomain);
        if (bool) {
            c.a("WebViewer", new Object[] { "Set Cookie, Send Message" });
            this.mHandler.sendEmptyMessageDelayed(0, 100L);
        }
        else {
            c.a("WebViewer", new Object[] { "No Cookie, Send Request" });
            com.qihoo.gamecenter.sdk.common.web.a.a(this.mWebView, paramString);
        }
    }

    public void setReload(boolean paramBoolean) {
        this.mReloadAble = paramBoolean;
    }

    public void setOnWebViewClient(c paramc) {
        this.mWebViewClient.a(paramc);
    }

    public void addJavascriptInterface(Object paramObject, String paramString) {
        this.mWebView.addJavascriptInterface(paramObject, paramString);
    }

    public void setOnWebChromeClient(b paramb) {
        this.mWebChromeClient.a(paramb);
    }

    public void setUserAgentString(String paramString) {
        this.mWebView.getSettings().setUserAgentString(paramString);
    }

    public void clear() {
        if (this.mHandler.hasMessages(0)) {
            this.mHandler.removeMessages(0);
            this.mIsSynedMsgRemoved = true;
        }
        if (this.mHandler.hasMessages(1)) {
            this.mHandler.removeMessages(1);
            this.mIsTimeoutMsgRemoved = true;
        }
        com.qihoo.gamecenter.sdk.common.web.a.a(this.mContainer);
    }

    public void destory() {
        clear();
        if (this.mNetController != null)
            this.mNetController.a();
        com.qihoo.gamecenter.sdk.common.web.a.a(this.mWebView);
    }

    public void pause() {
        WebView.disablePlatformNotifications();
        if (this.mHandler.hasMessages(0)) {
            this.mHandler.removeMessages(0);
            this.mIsSynedMsgRemoved = true;
        }
        if (!(this.mHandler.hasMessages(1)))
            return;
        this.mHandler.removeMessages(1);
        this.mIsTimeoutMsgRemoved = true;
    }

    public void resume() {
        WebView.enablePlatformNotifications();
        if (this.mIsSynedMsgRemoved) {
            this.mHandler.sendEmptyMessage(0);
            this.mIsSynedMsgRemoved = false;
        }
        if (!(this.mIsTimeoutMsgRemoved))
            return;
        this.mHandler.sendEmptyMessage(1);
        this.mIsTimeoutMsgRemoved = false;
    }

    public boolean goBack() {
        try {
            if (this.mWebView.canGoBack()) {
                this.mWebView.goBack();
                return true;
            }
        } catch (Exception localException) {}
        clear();
        return false;
    }

    public String getUrl() {
        try {
            return this.mWebView.getUrl();
        } catch (Exception localException) {}
        return null;
    }

    public String getOriginalUrl() {
        try {
            return this.mWebView.getOriginalUrl();
        } catch (Exception localException) {}
        return null;
    }

    public void setCookies(String[] paramArrayOfString) {
        setCookies(paramArrayOfString, null, null, null);
    }

    public void setCookies(String[] paramArrayOfString, String paramString1, String paramString2, String paramString3) {
        this.mCookies = paramArrayOfString;
        this.mExpires = paramString1;
        this.mPath = paramString2;
        this.mDomain = paramString3;
    }

    public static abstract interface b {
        public abstract void a(WebView paramWebView, int paramInt);

        public abstract void a(ValueCallback paramValueCallback, String paramString1, String paramString2);
    }

    public static abstract interface c {
        public abstract void a(WebView paramWebView, String paramString);

        public abstract void a(WebView paramWebView, String paramString, Bitmap paramBitmap);

        public abstract void a(WebView paramWebView, int paramInt, String paramString1, String paramString2);

        public abstract boolean b(WebView paramWebView, String paramString);
    }

    private class d extends WebChromeClient {
        private WebViewer.b b = null;

        public void a(WebViewer.b paramb) {
            this.b = paramb;
        }

        public void openFileChooser(ValueCallback paramValueCallback, String paramString1, String paramString2) {
            if (this.b != null)
                this.b.a(paramValueCallback, paramString1, paramString2);
            else
                super.openFileChooser(paramValueCallback, paramString1, paramString2);
        }

        public void onProgressChanged(WebView paramWebView, int paramInt) {
            if (paramInt >= 50)
                WebViewer.this.mWebView.setVisibility(0);
            if (this.b == null)
                return;
            this.b.a(paramWebView, paramInt);
        }
    }

    private class e extends WebViewClient {
        private WebViewer.c b = null;

        public void a(WebViewer.c paramc) {
            this.b = paramc;
        }

        public void onPageStarted(WebView paramWebView, String paramString, Bitmap paramBitmap) {
            c.a("WebViewerChromeClient", new Object[] { "onPageStarted() ----------------> ", paramString });
            if (this.b != null)
                this.b.a(paramWebView, paramString, paramBitmap);
            WebViewer.this.mHandler.sendEmptyMessageDelayed(1, 20000L);
        }

        public boolean shouldOverrideUrlLoading(WebView paramWebView, String paramString) {
            if (WebViewer.this.mReloadAble)
                WebViewer.access$202(WebViewer.this, paramString);
            if ((this.b != null) && (this.b.b(paramWebView, paramString)))
                return true;
            com.qihoo.gamecenter.sdk.common.web.a.a(paramWebView, paramString);
            return true;
        }

        public void onReceivedError(WebView paramWebView, int paramInt, String paramString1, String paramString2) {
            c.a("WebViewerChromeClient", new Object[] { "onReceivedError() ----------------> ", paramString1 });
            if (WebViewer.this.mHandler.hasMessages(1))
                WebViewer.this.mHandler.removeMessages(1);
            q.a(WebViewer.this.mContainer, paramString1, 0, 80);
            if (this.b == null)
                return;
            this.b.a(paramWebView, paramInt, paramString1, paramString2);
        }

        public void onPageFinished(WebView paramWebView, String paramString) {
            c.a("WebViewerChromeClient", new Object[] { "onPageFinished() ----------------> ", paramString });
            if (WebViewer.this.mHandler.hasMessages(1))
                WebViewer.this.mHandler.removeMessages(1);
            if (this.b == null)
                return;
            this.b.a(paramWebView, paramString);
        }

        @SuppressLint({ "Override" })
        @TargetApi(8)
        public void onReceivedSslError(WebView paramWebView, SslErrorHandler paramSslErrorHandler,
                SslError paramSslError) {
            c.a("WebViewerChromeClient", new Object[] { "onReceivedSslError" });
            paramSslErrorHandler.proceed();
        }
    }

    private static class a extends Handler {
        private WeakReference a;

        public a(WebViewer paramWebViewer) {
            this.a = new WeakReference(paramWebViewer);
        }

        public void handleMessage(Message paramMessage) {
            WebViewer localWebViewer = (WebViewer) this.a.get();
            if (localWebViewer == null)
                return;
            switch (paramMessage.what) {
            case 0:
                localWebViewer.trySyncedCookie();
                break;
            case 1:
                localWebViewer.showTimeOutView();
            }
        }
    }
}